package com.center.growcenterserver.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.center.growcenterserver.common.R;
import com.center.growcenterserver.mapper.GrpMapper;
import com.center.growcenterserver.mapper.StudentMapper;
import com.center.growcenterserver.model.domain.GraduateResearchProcess;
import com.center.growcenterserver.model.domain.Student;
import com.center.growcenterserver.service.GrpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

@Slf4j
@Service
public class GrpServiceImpl extends ServiceImpl<GrpMapper, GraduateResearchProcess> implements GrpService {

    @Autowired
    private GrpMapper grpMapper;

    @Override
    public R allContents(IPage<GraduateResearchProcess> page) {
        // 执行分页查询
        IPage<GraduateResearchProcess> resultPage = grpMapper.selectPage(page, null); // 使用默认 Wrapper 或者 new QueryWrapper<>()

        return R.success("查询成功", resultPage);
    }

    @Override
    public R getAllJobmessage(String jobname) {

//        jobname = "C++";
        StringBuilder output = new StringBuilder();


        try {
            String[] arguments = new String[] {
                    "D:\\anaconda3\\python.exe",
                    "D:\\python-workplace\\爬虫练习demo001\\BOSS直聘\\jiuye.py",

                    String.valueOf(jobname),
                    };

            // 执行py文件，注意：这里的命令都最好用绝对路径，到底用哪一个环境的下的Python，比如env下面的tf2的环境；具体执行哪个python文件，也是绝对路径
            Process proc = Runtime.getRuntime().exec(arguments); // 执行py文件，
            // 用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));



            //输出结果到Java的控制台
            String line = null;

            while ((line = in.readLine()) != null) {
                System.out.println(line);
                output.append(line);
            }

            in.close();
            proc.waitFor();

            JSONObject response = JSON.parseObject(output.toString());


            if (response != null) {
                if (response.containsKey("average_salary") && response.containsKey("work_city")) {
                    Double averageSalary = response.getDouble("average_salary");
                    String workCity = response.getString("work_city");
                    return R.success("success!!!!",new HashMap<String, Object>() {{
                        put("average_salary", averageSalary);
                        put("work_city", workCity);
                    }});
                } else if (response.containsKey("error")) {
                    return R.error(response.getString("error"));
                } else {
                    return R.error("无效的响应格式");
                }
            }


            
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return R.error("未得到有效回应");
    }



    @Override
    public R getJobCourse(String jobname) {


        return null;
    }


}
